package com.yeyks.hotel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.appcommon.param.room.CalendarVIPDataParam;
import com.yeyks.common.utils.DateUtils;
import com.yeyks.hotel.dal.dao.RoomCalendarVipInfoMapper;
import com.yeyks.hotel.dal.dao.RoomInfoMapper;
import com.yeyks.hotel.dal.domain.RoomCalendarVipInfo;
import com.yeyks.hotel.dal.domain.RoomInfo;
import com.yeyks.hotel.param.UpdateRoomCalendarBatchParam;
import com.yeyks.hotel.service.RoomCalendarVipInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;


/**
 * <p>
 * 房间日历信息表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Service
@Slf4j
public class RoomCalendarVipInfoServiceImpl extends ServiceImpl<RoomCalendarVipInfoMapper, RoomCalendarVipInfo> implements RoomCalendarVipInfoService {

    @Autowired
    private RoomCalendarVipInfoMapper roomCalendarVipInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    //批量添加VIP房间日期信息
    @Override
    public void createRoomCalendarVipInfoDo(List<RoomInfo> roomInfos) {
        //获取日期
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        //  起始日期
        Date startTime = calendar.getTime();
        calendar.add(Calendar.DAY_OF_YEAR, 60);
        //  截止日期(不包含)
        Date endTime = calendar.getTime();
        ArrayList<RoomCalendarVipInfo> roomCalendarInfoVipNews = new ArrayList<>();
        //排除不需要操作的room
        ArrayList<Integer> idList = new ArrayList<>();
        for (RoomInfo roomInfo : roomInfos) {
            idList.add(roomInfo.getId());
        }
        List<RoomCalendarVipInfo> roomCalendarInfos = baseMapper.selectForCreateRoomCalendarInfoDo(idList, startTime);
        Iterator<RoomInfo> iterator = roomInfos.iterator();
        while (iterator.hasNext()) {
            RoomInfo roomInfo = iterator.next();
            boolean flag = true;
            for (RoomCalendarVipInfo roomCalendarVipInfo : roomCalendarInfos) {
                if (roomCalendarVipInfo.getRoomId().equals(roomInfo.getId())) {
                    Date date = null;
                    date = roomCalendarVipInfo.getDate();
                    calendar.setTime(date);
                    calendar.add(Calendar.DAY_OF_YEAR, 1);
                    date = calendar.getTime();
                    //添加记录
                    createObject(calendar, endTime, roomCalendarInfoVipNews, roomInfo, date);
                    flag = false;
                    break;
                }
            }
            if (flag) {
                Date date = startTime;
                createObject(calendar, endTime, roomCalendarInfoVipNews, roomInfo, date);
            }
        }
        //批量添加
        if (roomCalendarInfoVipNews.size() > 0) {
            baseMapper.insertBatchCalendar(roomCalendarInfoVipNews);
            System.out.print("批量家庭vip记录:"+roomCalendarInfoVipNews.size());
        }
    }

    /**
     * 修改房态状态为未生效
     *
     * @param id
     * @param status
     */
    @Override
    public int updateRoomCalendarStatus(Integer id, Integer status) {
        return baseMapper.updateRoomCalendarStatus(id, status);
    }

    @Override
    public void initVIPRoomCalendar(RoomInfo roomInfo) {
        //初始化30天房态信息
        List<RoomCalendarVipInfo> initCalendarInfoList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        for (int i = 1; i <= 60; i++) {
            RoomCalendarVipInfo roomCalendarInfoNew = new RoomCalendarVipInfo();
            roomCalendarInfoNew.setHotelId(roomInfo.getHotelId());
            roomCalendarInfoNew.setRoomId(roomInfo.getId());
            roomCalendarInfoNew.setDate(calendar.getTime());
            roomCalendarInfoNew.setSalePrice(roomInfo.getVipDiscountPrice());
            roomCalendarInfoNew.setPurcPrice(roomInfo.getVipPrice());
            roomCalendarInfoNew.setCount(roomInfo.getVipCount());
            roomCalendarInfoNew.setLockCount(0);
            roomCalendarInfoNew.setStatus(roomInfo.getVipStatus());
            //如果未找到房态记录则插入新的
            QueryWrapper<RoomCalendarVipInfo> queryWrapper = new QueryWrapper();
            queryWrapper.eq(RoomCalendarVipInfo.ROOM_ID,roomInfo.getId());
            queryWrapper.eq(RoomCalendarVipInfo.DATE,DateUtils.toYYMMddStr(calendar.getTime()));
            if(count(queryWrapper)==0)
            {
                initCalendarInfoList.add(roomCalendarInfoNew);
            }
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        if (initCalendarInfoList!=null&&initCalendarInfoList.size()>0)
        {
            baseMapper.insertBatchCalendar(initCalendarInfoList);
        }
    }

    @Override
    public AjaxResponseBody updateRoomCalendarBatch(UpdateRoomCalendarBatchParam param) {
        List<CalendarVIPDataParam> jsonData = JSONObject.parseArray(param.getCalendarData(),CalendarVIPDataParam.class);
        List<RoomCalendarVipInfo> calendarInfoList = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(jsonData)) {
            for (CalendarVIPDataParam dataParam : jsonData) {
                RoomCalendarVipInfo roomCalendarInfo = new RoomCalendarVipInfo();
                roomCalendarInfo.setHotelId(param.getHotelId())
                        .setRoomId(param.getRoomId())
                        .setId(dataParam.getId())
                        .setDate(dataParam.getDate())
                        .setPurcPrice(dataParam.getPurcPrice())
                        .setSalePrice(dataParam.getSalePrice())
                        .setStatus(dataParam.getStatus())
                        .setCount(dataParam.getCount());
                calendarInfoList.add(roomCalendarInfo);
            }
        }
        if (!CollectionUtils.isEmpty(calendarInfoList)) {
            int count = roomCalendarVipInfoMapper.updateBatchCalendar(calendarInfoList);
            //上架房态操作则上架vip房间
            if(calendarInfoList.get(0).getStatus()==1)
            {
                RoomInfo roomInfo = roomInfoMapper.selectByPrimaryKey(param.getRoomId());
                roomInfo.setVipStatus(1);
                roomInfoMapper.updateByPrimaryKey(roomInfo);
            }
            if (count > 0) {
                log.info("批量更新数据成功，更新行数为[{}]", count);
                return AjaxResponseBody.success("更新客房信息成功");
            }
        }
        return AjaxResponseBody.failed("更新客房信息失败");
    }

    @Override
    public List<RoomCalendarVipInfo> selectVIPRoomCalendar(Integer roomId) {
        Calendar calendar = Calendar.getInstance();
        //  起始日期
        Date startTime = calendar.getTime();
        calendar.add(Calendar.DAY_OF_YEAR, 30);
        //  截止日期(不包含)
        Date endTime = calendar.getTime();
        QueryWrapper<RoomCalendarVipInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RoomCalendarVipInfo::getRoomId, roomId)
                .ge(RoomCalendarVipInfo::getDate, DateUtils.formatDate(startTime))
                .le(RoomCalendarVipInfo::getDate, DateUtils.formatDate(endTime));
        List<RoomCalendarVipInfo> RoomCalendarInfoList = list(queryWrapper);
        return RoomCalendarInfoList;
    }

    private void createObject(Calendar calendar, Date endTime, ArrayList<RoomCalendarVipInfo> roomCalendarInfoVipNews, RoomInfo roomInfo, Date date) {
        //添加记录
        while (date.before(endTime)) {
            RoomCalendarVipInfo roomCalendarVipInfoNew = new RoomCalendarVipInfo();
            roomCalendarVipInfoNew.setHotelId(roomInfo.getHotelId());
            roomCalendarVipInfoNew.setRoomId(roomInfo.getId());
            roomCalendarVipInfoNew.setDate(date);
            roomCalendarVipInfoNew.setPurcPrice(roomInfo.getVipPrice());
            roomCalendarVipInfoNew.setSalePrice(roomInfo.getVipDiscountPrice());
            roomCalendarVipInfoNew.setCount(roomInfo.getCount());
            roomCalendarVipInfoNew.setLockCount(0);
            roomCalendarVipInfoNew.setStatus(roomInfo.getVipStatus());
            //逐条生成房态记录
            //roomCalendarVipInfoMapper.insert(roomCalendarVipInfoNew);
            roomCalendarInfoVipNews.add(roomCalendarVipInfoNew);
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            date = calendar.getTime();
        }
    }
}
